package com.example.todoapp.http.controllers;

import com.example.todoapp.data.TodoItemRepository;
import com.example.todoapp.data.entities.TodoItem;
import com.example.todoapp.http.ResponseStruct;
import com.example.todoapp.http.TodoItemRequest;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.support.PageableUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Optional;

@RestController
public class TodoItemController {

    @Autowired
    TodoItemRepository todoItemRepository;

    @GetMapping("/todo-list")
    public ResponseStruct list(@RequestParam MultiValueMap<String, Object> params) {
        // pagination
        int page = (int) Optional.ofNullable(params.getFirst("page")).orElse(1);
        int size = (int) Optional.ofNullable(params.getFirst("size")).orElse(10);
        PageRequest pageRequest = PageRequest.of(page, size);
        // filter
        Specification<TodoItem> specification = (root, query, criteriaBuilder) -> {
            return null;
        };
        if(params.containsKey("title")){

        }

        Iterable<TodoItem> list = todoItemRepository.findAll();

        return ResponseStruct.wrap(list);
    }

    @PostMapping("/todo")
    public ResponseStruct store(@RequestBody TodoItemRequest request) {

        TodoItem todoItem = new TodoItem();
        BeanUtils.copyProperties(request, todoItem);
        todoItem.setCreatedAt(LocalDateTime.now());

        todoItemRepository.save(todoItem);

        return ResponseStruct.wrap(todoItem);
    }

    @GetMapping("/todo/{id}")
    public ResponseStruct show(@PathVariable long id) {
        TodoItem todoItem = todoItemRepository.findFirstById(id);

        if (todoItem == null) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("id", id);
            return new ResponseStruct(-1, "todo-item not found", data);
        }

        return ResponseStruct.wrap(todoItem);
    }

    @PostMapping("/todo/{id}")
    public ResponseStruct update(@RequestBody TodoItemRequest request, @PathVariable long id) {
        TodoItem todoItem = todoItemRepository.findFirstById(id);

        if (todoItem == null) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("id", id);
            return new ResponseStruct(-1, "todo-item not found", data);
        }

        if (request.getTitle() != null) {
            todoItem.setTitle(request.getTitle());
        }
        if (request.getDescription() != null) {
            todoItem.setDescription(request.getDescription());
        }
        if (request.getDate() != null) {
            todoItem.setDate(request.getDate());
        }
        todoItem.setUpdatedAt(LocalDateTime.now());

        todoItemRepository.save(todoItem);

        return ResponseStruct.wrap(todoItem);
    }

    @PostMapping("/todo/delete/{id}")
    public ResponseStruct delete(@PathVariable long id) {
        TodoItem todoItem = todoItemRepository.findFirstById(id);

        if (todoItem == null) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("id", id);
            return new ResponseStruct(-1, "todo-item not found", data);
        }

        todoItemRepository.delete(todoItem);

        return ResponseStruct.wrap(data -> data.put("id", id));
    }
}
